package com.sailors.common.task.provider.domian.convert;

import com.sailors.common.task.provider.domian.model.entity.FileTaskEntity;
import com.sailors.common.task.provider.domian.model.entity.FileTaskEntityCommonProperty;
import com.sailors.common.task.provider.domian.model.val.TaskProValueObj;
import com.sailors.common.task.provider.faced.model.dto.execute.FileTaskCreateExecuteDto;
import com.sailors.common.task.provider.faced.model.dto.execute.FileTaskFailedExecuteDto;
import com.sailors.common.task.provider.faced.model.dto.execute.RefreshProcessExecuteDto;
import com.sailors.common.task.provider.faced.model.enums.TaskStatusEnum;
import com.sailors.common.task.provider.faced.model.vo.TaskProLogVo;
import com.sailors.common.task.provider.infra.repository.model.FileTaskLogPo;
import org.apache.commons.lang3.StringUtils;
import org.mapstruct.Mapper;
import org.mapstruct.Mappings;
import org.mapstruct.factory.Mappers;

import java.util.Date;
import java.util.List;


/**
 * File task entity conver
 *
 * @description:
 * @author: cyj
 * @date 2023 -02-22 00:19:54
 */
@Mapper
public interface FileTaskEntityConver {
    /**
     * 默认的方式
     */
    FileTaskEntityConver INSTANCE = Mappers.getMapper(FileTaskEntityConver.class);

    /**
     * 装填失败信息
     * @param fileTaskEntity
     * @param fileTaskFailedExecuteDto
     */
    static void failedAssemable(FileTaskEntity fileTaskEntity, FileTaskFailedExecuteDto fileTaskFailedExecuteDto) {
        fileTaskEntity.getCommonProperty().setStatus(TaskStatusEnum.FAILED.getCode());
        fileTaskEntity.getCommonProperty().setOverTime(new Date());
        fileTaskEntity.getCommonProperty().setReason(fileTaskFailedExecuteDto.getReason());
        fileTaskEntity.getCommonProperty().setReasonUrl(fileTaskFailedExecuteDto.getReasonUrl());
    }

    @Mappings({
    })
    FileTaskEntityCommonProperty dto2Property(FileTaskCreateExecuteDto fileTaskCreateExecuteDto);

    @Mappings({
    })
    FileTaskEntityCommonProperty po2Property(FileTaskLogPo fileTaskLogPo);

    @Mappings({
    })
    List<TaskProValueObj> proVos2Vals(List<TaskProLogVo> taskProLogvVos);

    @Mappings({
    })
    TaskProValueObj refreshProcessExecuteDto2Val(RefreshProcessExecuteDto refreshProcessExecuteDto);

    default void refreshProcess(RefreshProcessExecuteDto refreshProcessExecuteDto, FileTaskEntity fileTaskEntity) {
        if (StringUtils.isNotEmpty(refreshProcessExecuteDto.getSourceFileUrl())) {
            fileTaskEntity.getCommonProperty().setSourceFileUrl(refreshProcessExecuteDto.getSourceFileUrl());

        }
        if (null != refreshProcessExecuteDto.getCount()) {
            fileTaskEntity.getCommonProperty().setCount(refreshProcessExecuteDto.getCount());
        }
    }
}
